//
//  SQMainListViewController.m
//  pai
//
//  Created by pro on 14-11-6.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQMainListViewController.h"
#import "SQOneAuctionViewController.h"
#import "SQLowAuctionViewController.h"
#import "SQSecKillViewController.h"
#import "SQShopViewController.h"
#import "SQLowAuctionDetailViewController.h"
#import "SQGoodsContentViewController.h"
#import "ASIHTTPRequest.h"
#import "SQSocket.h"

@interface SQMainListViewController ()

@end

@implementation SQMainListViewController

@synthesize scrollView = _scrollView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"首页"];
    [self getList];
//    if (self.HUD == nil) {
//        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//        self.HUD.mode = MBProgressHUDModeIndeterminate;
//        self.HUD.margin = 10.f;
//        self.HUD.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
//        self.HUD.delegate = self;
//        [self.HUD showWhileExecuting:@selector(getList:) onTarget:self withObject:nil animated:YES];
//    }
}
-(void)getList
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/recommendations.json", IMAGESERVICE]];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    [request startSynchronous];
    
    NSError *error = [request error];
    
    if (!error) {
        
        NSString *response = [request responseString];
        NSLog(@"%@",response);
        
        NSData *data= [response dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        self.lowMArr = [NSMutableArray array];
        self.lowMArr = [dic objectForKey:@"sku"];
        
        self.proMArr = [NSMutableArray array];
        self.proMArr = [dic objectForKey:@"product"];
        //NSString* imageUrl = [NSString stringWithFormat:@"%@%@",IMAGESERVICE,[dic objectForKey:@"home"]];
        [self initlist];
    }
}
-(void)initlist
{
    _scrollView = [[UIScrollView alloc]init];
    //[_scrollView setBackgroundColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.5]];
    _scrollView.frame = CGRectMake(0, self.MyNavigationBar.frame.origin.y+self.MyNavigationBar.frame.size.height, 320, [[UIScreen  mainScreen] bounds].size.height - 44 - self.MyNavigationBar.frame.origin.y-self.MyNavigationBar.frame.size.height);
    _scrollView.pagingEnabled = NO;
    _scrollView.delegate = self;
    _scrollView.bounces = YES;
    [self.view addSubview:_scrollView];
    
    //最低价竞拍
    
    SQMainCell* first = [SQMainCell initmyview];
    [first.titleLabel setText:@"最低价竞拍"];
    [first.title_backgroundView setBackgroundColor:[UIColor  colorWithRed:0.9 green:0.1 blue:0.1 alpha:1]];
    [first setInfo:self.lowMArr];
    [_scrollView addSubview:first];
    first.delegate = self;
    
//    //一元购
//    SQMainCell* second = [SQMainCell initmyview];
//    [second.titleLabel setText:@"一元购"];
//    [second.title_backgroundView setBackgroundColor:[UIColor  colorWithRed:0.1 green:0.9 blue:0.1 alpha:1]];
//    [second setDic:nil];
//    second.frame = CGRectMake(0, 250, 320, 280);
//    [_scrollView addSubview:second];
//    second.delegate = self;
//    
//    //秒杀
//    SQMainCell* third = [SQMainCell initmyview];
//    [third.titleLabel setText:@"秒杀"];
//    [third.title_backgroundView setBackgroundColor:[UIColor  colorWithRed:0.1 green:0.1 blue:0.9 alpha:1]];
//    [third setDic:nil];
//    third.frame = CGRectMake(0, 500, 320, 280);
//    [_scrollView addSubview:third];
//    third.delegate = self;
    
    //兑换
    SQMainCell* forth = [SQMainCell initmyview];
    [forth.titleLabel setText:@"兑换"];
    [forth.title_backgroundView setBackgroundColor:[UIColor  colorWithRed:0.5 green:0.5 blue:0.5 alpha:1]];
    [forth setInfo:self.proMArr];
    forth.frame = CGRectMake(0, 250, 320, 280);
    [_scrollView addSubview:forth];
    forth.delegate = self;
    
    _scrollView.contentSize = CGSizeMake(320, 550);
}
-(void)clickMore:(NSString *)title
{
    if ([title isEqualToString:@"最低价竞拍"]) {
        SQLowAuctionViewController* lavc = [[SQLowAuctionViewController alloc]init];
        [self.navigationController pushViewController:lavc animated:YES];
    }else if ([title isEqualToString:@"一元购"]){
        SQOneAuctionViewController* oavc = [[SQOneAuctionViewController alloc]init];
        [self.navigationController pushViewController:oavc animated:YES];
    }else if ([title isEqualToString:@"秒杀"]){
        SQSecKillViewController* skvc =[[SQSecKillViewController alloc]init];
        [self.navigationController pushViewController:skvc animated:YES];
    }else if ([title isEqualToString:@"兑换"]){
        SQShopViewController* svc = [[SQShopViewController alloc]init];
        [self.navigationController pushViewController:svc animated:YES];
    }else{
        NSLog(@"error");
    }
}
-(void)clickImagewithType:(NSString *)type Dictionary:(NSDictionary *)dic
{
    NSLog(@"click image");
    if ([type isEqualToString:@"最低价竞拍"]) {
        //最低价信息
        auct_sku_item_t auction;
        auction.stdate = [[dic objectForKey:@"timeon"]intValue];
        auction.eddate = [[dic objectForKey:@"timeoff"]intValue];
        NSLog(@"dic:%@",dic);
        strcpy(auction.sku_code, [[dic objectForKey:@"sku_code"] UTF8String]);
        strcpy(auction.sku_name, [[dic objectForKey:@"sku_name"] UTF8String]);
        strcpy(auction.serialnum, [[dic objectForKey:@"serialnum"] UTF8String]);
        
        SQLowAuctionDetailViewController* ladvc = [[SQLowAuctionDetailViewController alloc]init];
        ladvc.item = auction;
        ladvc.imageDic = [self getskuImage:[dic objectForKey:@"sku_code"]];
        [ladvc getInfo];
        [self.navigationController pushViewController:ladvc animated:YES];
    }else if ([type isEqualToString:@"一元购"]){
        
    }else if ([type isEqualToString:@"秒杀"]){
        
    }else if ([type isEqualToString:@"兑换"]){
        //兑换信息
        NSLog(@"%@",dic);
        product_item_t product;
        product.product_id = [[dic objectForKey:@"id"]intValue];
        strcpy(product.intro, [[dic objectForKey:@"product_intro"] UTF8String]);
        strcpy(product.title, [[dic objectForKey:@"name"] UTF8String]);
        
        SQGoodsContentViewController* svc = [[SQGoodsContentViewController alloc]init];
        svc.item = product;
        svc.imageDic = [self getproductImage:[dic objectForKey:@"id"]];
        [svc getInfo];
        [self.navigationController pushViewController:svc animated:YES];
    }else{
        NSLog(@"error");
    }
}
-(NSDictionary*)getskuImage:(NSString*)skucode
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/skus/%@/photos.json", IMAGESERVICE,skucode]];
    NSLog(@"get image url:%@",url);
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    [request startSynchronous];
    
    NSError *error = [request error];
    
    if (!error) {
        
        NSString *response = [request responseString];
        NSLog(@"%@",response);
        
        NSData *data= [response dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        return dic;
    }
    return nil;
}
-(NSDictionary*)getproductImage:(NSString*)productcode
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/products/%@/photos.json", IMAGESERVICE,productcode]];
    NSLog(@"get image url:%@",url);
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    [request startSynchronous];
    
    NSError *error = [request error];
    
    if (!error) {
        
        NSString *response = [request responseString];
        NSLog(@"%@",response);
        
        NSData *data= [response dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        return dic;
    }
    return nil;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
